﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NHibernate;
using NHibernate.Cfg;

namespace Rodunu.Movies.Data
{
    public class DBHelper
    {
        private const string CurrentSessionKey = "nhibernate.current_session";
        private static ISessionFactory _sessionFactory;

        static DBHelper()
        {
            _sessionFactory = BuildConfiguration().Configure().BuildSessionFactory();
        }

        public static ISession GetCurrentSession()
        {
            HttpContext context = HttpContext.Current;
            var currentSession = context.Items[CurrentSessionKey] as ISession;

            currentSession = SessionFactory.OpenSession();
            //if (currentSession == null)
            //{
            //    currentSession = SessionFactory.OpenSession();
            //    context.Items[CurrentSessionKey] = currentSession;
            //}

            return currentSession;
        }

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    _sessionFactory = BuildConfiguration().Configure().BuildSessionFactory();
                }

                return _sessionFactory;
            }
        }

        public static Configuration BuildConfiguration()
        {
            Configuration conf;
            try
            {
                conf = new Configuration();
            }
            catch (Exception ex)
            {

                throw;
            }

            conf.Properties.Add(new KeyValuePair<string, string>("connection.connection_string",
                                                                 System.Configuration.ConfigurationManager.
                                                                     ConnectionStrings["Connection"].ToString()));
            conf.Properties.Add(new KeyValuePair<string, string>("dialect",
                                                                 System.Configuration.ConfigurationManager.
                                                                     AppSettings["Dialect"]));
            conf.Properties.Add(new KeyValuePair<string, string>("connection.provider",
                                                                 System.Configuration.ConfigurationManager.
                                                                     AppSettings["Provider"]));
            conf.AddAssembly("Rodunu.Movies.Contracts");

            return conf;
        }

        public static void CloseSession()
        {
            HttpContext context = HttpContext.Current;
            var currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                // No current session
                return;
            }

            currentSession.Close();
            context.Items.Remove(CurrentSessionKey);
        }

        public static void CloseSessionFactory()
        {
            if (_sessionFactory != null)
            {
                _sessionFactory.Close();
            }
        }
    }
}
